package com.springboot.file;

import com.springboot.file.fastdfs.FastDfsStorageService;
import com.springboot.file.ftp.FileCommonUtil;
import com.springboot.file.filebase.IFileStorageInterface;
import com.springboot.file.filedto.MultipartFileDto;
import com.springboot.file.ftp.FTPFileStorageService;
import com.springboot.file.oss.OssStorageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;

/**
 * @Classname BaseFileStoreFileUtil
 * @Description 文件类
 * @Version 1.0.0
 * @Date 2024/1/8 16:23
 * @Created by zyj
 */
@Component
public class BaseFileStoreFileUtil implements IFileStorageInterface {

    @Value("file.type:1")
    private String fileType;

    @Resource
    private FTPFileStorageService ftpFileStorageService;

    @Resource
    private FastDfsStorageService fastDfsStorageService;

    @Resource
    private OssStorageService ossStorageService;

    @Override
    public String uploadMultipartFile(MultipartFileDto multipartFileDto) throws FileException {
        MultipartFile multipartFile = multipartFileDto.getMultipartFile();
        if(multipartFile==null){
            throw new FileException("文件为空");
        }
        return getRealFileStore().uploadMultipartFile(multipartFileDto);
    }

    @Override
    public byte[] downloadFile(String filePath) throws FileException {
        if(StringUtils.isEmpty(filePath)){
            throw new FileException("文件为空");
        }
        return getRealFileStore().downloadFile(filePath);
    }

    @Override
    public boolean deleteFile(String filePath) throws FileException {
        if(StringUtils.isEmpty(filePath)){
            throw new FileException("文件为空");
        }
        return getRealFileStore().deleteFile(filePath);
    }

    @Override
    public String getFileLink(String filePath) throws FileException {
        if(StringUtils.isEmpty(filePath)){
            throw new FileException("文件为空");
        }
        return getRealFileStore().getFileLink(filePath);
    }

    @Override
    public String getDomain() {
        return getRealFileStore().getDomain();
    }


    public IFileStorageInterface getRealFileStore() {
        IFileStorageInterface iFileStorageInterface = null;
        switch (fileType){
            case FileCommonUtil.FTP_TYPE :
                iFileStorageInterface = ftpFileStorageService;
                break;
            case FileCommonUtil.FASTDFS_TYPE :
                iFileStorageInterface = fastDfsStorageService;
                break;
            case FileCommonUtil.OSS_TYPE :
                iFileStorageInterface = ossStorageService;
                break;
            default:
                iFileStorageInterface = ftpFileStorageService;
                break;
        }
        return iFileStorageInterface;
    }

    /**
     * 判断url是否为当前环境url
     *
     * @param url
     * @return
     */
    public Boolean isCurrentEnvUrl(String url) {
        if (url != null) {
            return url.contains(getRealFileStore().getDomain());
        }
        return false;
    }
}
